package com.yfjin_rtmp;

import android.media.projection.MediaProjection;
import android.util.Log;

import java.util.concurrent.LinkedBlockingQueue;

//推送层
class ScreenLive extends Thread {


    private LinkedBlockingQueue<RTMPPackage> queue = new LinkedBlockingQueue<>();

    private boolean isLiving;
    private String url;
    private MediaProjection mediaProjection;

    public void addPackage(RTMPPackage rtmpPackage) {
        if (!isLiving) {
            return;
        }
        queue.add(rtmpPackage);
    }


    public void startLive(String url, MediaProjection mediaProjection) {
        this.url=url;
        this.mediaProjection=mediaProjection;
        start();
    }

    @Override
    public void run() {
        super.run();

        if (!connect(url)) {
            Log.i("david", "run: ----------->推送失败");
            return;
        }

        VideoCodec videoCodec=new VideoCodec(this);
        videoCodec.stratLive(mediaProjection);

//        AudioCodec audioCodec=new AudioCodec(this);
//        audioCodec.startLive();

        isLiving = true;

        while (isLiving){
            RTMPPackage rtmpPackage=null;
            try {
                rtmpPackage=queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            int lenght=rtmpPackage.getBuffer().length;
            if (rtmpPackage.getBuffer()!=null&&lenght!=0){
                Log.i("123123","推送数据----->"+lenght);
                sendData(rtmpPackage.getBuffer(),lenght,rtmpPackage.getTms(),rtmpPackage.getType());
            }

        }
    }




    private native boolean connect(String url);

    private native boolean sendData(byte[] buffer, int len, long tms,int type);
}
